//
//  HLSquareViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLSquareViewController.h"
#import "HLHomeLabelRequest.h"
#import "HLSquareListViewController.h"
#import <JXCategoryView/JXCategoryView.h>

@interface HLSquareViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray<HLHomeLabelModel *>*groups;

@end

@implementation HLSquareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.request = [[HLHomeLabelRequest alloc] initWithType:HLHomeLabelTypeSquare];
    self.request.delegate = self;
    [self requestInit];
    
}

- (void)refresh
{
    [self requestInit];
}

- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.request start];
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    HLHomeLabelRequest *groupRequest = (HLHomeLabelRequest *)self.request;
    
    self.groups = groupRequest.labelList;
    self.categoryView.titles = [self.groups qmui_mapWithBlock:^NSString *(HLHomeLabelModel *item, NSInteger index) {
        return item.tagName;
    }];
    [self.categoryView reloadData];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}


- (void)initSubviews
{
    [super initSubviews];
    self.backgroundView = [[UIImageView alloc] initWithImage:UIImageMake(@"square_background")];
    [self.view addSubview:_backgroundView];
    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectZero];
    self.categoryView.delegate = self;
    self.categoryView.titleColor = [UIColorWhite colorWithAlphaComponent:0.5];
    self.categoryView.titleSelectedColor = UIColorWhite;
    self.categoryView.titleFont = UIFontMediumMake(16);
    self.categoryView.titleSelectedFont = UIFontBoldMake(20);
    self.categoryView.averageCellSpacingEnabled = NO;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.titleLabelStrokeWidthEnabled = YES;
    self.categoryView.cellSpacing = 25;
    self.categoryView.contentEdgeInsetLeft = 18;
    self.categoryView.backgroundColor = UIColorClear;
    [self.view addSubview:self.categoryView];
    
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(38);
        make.top.mas_equalTo(StatusBarHeightConstant + 4);
    }];
    
    self.categoryView.titleColorGradientEnabled = YES;
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorClear;
    lineView.indicatorHeight = 5.f;
    lineView.indicatorWidth = 12.f;
    lineView.indicatorCornerRadius = 1.5f;
    lineView.verticalMargin = 2;
    lineView.lineStyle = JXCategoryIndicatorLineStyle_Lengthen;
    UIImageView *bottom = [[UIImageView alloc] initWithImage:UIImageMake(@"tag_bottom_w")];
    [lineView addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    self.categoryView.indicators = @[lineView];

    
    
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_categoryView.mas_bottom).offset(2);
        make.bottom.mas_equalTo(-TabBarHeight);
    }];
    
    self.categoryView.listContainer = self.listContainerView;
}


#pragma mark - JXCategoryViewDelegate

// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.groups.count;
}
// 根据下标 index 返回对应遵守并实现 `JXCategoryListContentViewDelegate` 协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    HLSquareListViewController *listVC = [[HLSquareListViewController alloc] initWithGroup:self.groups[index]];
    return listVC;

}


@end
